package com.tuanzi.loan.business.repository.wallet.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanUserWalletWithdrawRecord.LOAN_USER_WALLET_WITHDRAW_RECORD;

import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.repository.wallet.custom.LoanUserWalletWithdrawRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.wallet.LoanUserWalletWithdrawRecordRequest;
import com.tuanzi.loan.vo.response.wallet.LoanUserWalletWithdrawRecordResponse;
import com.tuanzi.server.loan_base.constant.wallet.LoanUserWalletWithdrawRecordStatus;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

@SuppressWarnings({ "rawtypes" })
@Repository
public class LoanUserWalletWithdrawRecordRepositoryImpl extends JOOQBaseRepository implements LoanUserWalletWithdrawRecordRepositoryCustom{
	
	@Override
	protected TableImpl getTable() {
		return LOAN_USER_WALLET_WITHDRAW_RECORD;
	}
	
	@Override
	public Page<LoanUserWalletWithdrawRecordResponse> list(
			LoanUserWalletWithdrawRecordRequest searchRequest, Pageable pageable) {
		
		fields(LOAN_USER_WALLET_WITHDRAW_RECORD.fields());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.MOBNUM, searchRequest.getMobnum());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.PRODUCT_ID, searchRequest.getProductId());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.CHANNEL_CODE, searchRequest.getChannelCode());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.PLATFORM, searchRequest.getPlatform());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.WITHDRAW_TYPE, searchRequest.getWithdrawType());
		eq(LOAN_USER_WALLET_WITHDRAW_RECORD.STATUS, searchRequest.getStatus());
		between(LOAN_USER_WALLET_WITHDRAW_RECORD.CREATE_TIME, searchRequest.getStartDate(), 
				searchRequest.getEndDate() == null ? searchRequest.getEndDate() : 
					DateUtils.formatDateToYyyyMMdd(searchRequest.getEndDate()) + " 23:59:59");
		orderBy(LOAN_USER_WALLET_WITHDRAW_RECORD.CREATE_TIME, Sort.Direction.DESC);
		
		 return search(pageable, record -> {
			 LoanUserWalletWithdrawRecordResponse response = new LoanUserWalletWithdrawRecordResponse();
			 
			    response.setId(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.ID));
			    response.setCreateTime(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.CREATE_TIME));
			    response.setProductId(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.PRODUCT_ID));
			    response.setChannelCode(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.CHANNEL_CODE).toString());
			    response.setPlatform(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.PLATFORM));
			    response.setMobnum(PhoneNumNideUtil.hideNum(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.MOBNUM)));
			    response.setAmount(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.AMOUNT).floatValue());
			    response.setWithdrawType(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.WITHDRAW_TYPE).intValue());
			    response.setWithdrawAccount(PhoneNumNideUtil.hideNum(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.WITHDRAW_ACCOUNT)));
			    response.setOrderId(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.ORDER_ID));
			    response.setStatus(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.STATUS).intValue());
			    response.setReturnBalance(record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.STATUS).intValue() == LoanUserWalletWithdrawRecordStatus.FAILED 
			    		|| record.get(LOAN_USER_WALLET_WITHDRAW_RECORD.STATUS).intValue() == LoanUserWalletWithdrawRecordStatus.INVALID);
	            
	            return response;
	        });
		
	}

	


}
